package com.isoft.controller;


import com.isoft.common.Result;
import com.isoft.entity.User;
import com.isoft.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserinfoController {
    @Autowired
    private UserMapper userMapper;

    @DeleteMapping("/delete")
    public Result<?> deleteUser(@RequestParam("jobnum") String jobnum) {
        User existingUser = userMapper.select(jobnum);
        if(existingUser != null){
            userMapper.delete(jobnum);
            return Result.success();
        }
        return Result.error("1","用户删除失败！");
    }
    @PutMapping("/update")
    public Result<?> updateUser(@RequestBody User user) {
        User existingUser = userMapper.haveUser(user);
        if(existingUser != null){
            userMapper.update(user);
            return Result.success();
        }
        return Result.error("1","用户更新失败！");
    }
    @GetMapping("/select")
    public Result<?> selectUser(@RequestParam("jobnum") String jobnum) {
        User existingUser = userMapper.select(jobnum);
        if(existingUser != null){
            return Result.successtwo(existingUser);
        }
        return Result.error("1","用户不存在，用户查询失败！");
    }
    @GetMapping("/selectAll")
    public List<User> selectAllUsers(){
        return userMapper.getAllUsers();
    }

}
